﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using G.Model;

namespace GameLibrary.Internal.Converters
{
    class ReadOnlyCollectionConverter : StringConverter
    {
        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        {
            if (sourceType == typeof(string)) {
                return true;
            }
            return base.CanConvertFrom(context, sourceType);
        }

        public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
        {
            if (destinationType == typeof(ICollection<GameLocation>))
            {
                return true;
            }
            return base.CanConvertTo(context, destinationType); 
        }

        public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
        {
            return base.ConvertFrom(context, culture, value);
        }

        public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
        {
            if (destinationType == typeof(System.String) &&
                    value is ICollection<GameLocation>)
            {
                String result = "[";
                ICollection<GameLocation> so = (ICollection<GameLocation>)value;
                foreach (GameLocation p in so)
                {
                    result += "" + new LocationConverter().ConvertToString(p) + "";
                }
                return result + "]";
            }
            return base.ConvertTo(context, culture, value, destinationType);
        }

    }
}
